#include "TimeTable.h"

TimeTable::TimeTable(Time s, Time e, Date d, int delta, int runningTime)
    : date(d)
{
    dueItems = new TimeTableItem[20];
    int n = (e - s) / (delta + runningTime);
    number = n;
    int i;
    for(i=0; i < n; i++)
    {
        dueItems[i].setStartTime(s);
        dueItems[i].setEndTime(s+runningTime);
        dueItems[i].setDirection(i % 2);
        s = s + delta + runningTime;
    }
}

RunningTable::RunningTable(Time s, Time e, Date d, int delta, int runningTime)
    : TimeTable(s, e, d, delta, runningTime)
{
    actualItems = new TimeTableItem[20];
    int i;
    for(i=0; i < number; i++)
    {
        actualItems[i].setStartTime(s);
        actualItems[i].setEndTime(s+runningTime);
        actualItems[i].setDirection(i % 2);
        s = s + delta + runningTime;
    }
}

void RunningTable::setTimeItem(int num, const Time& start, const Time& end) {
    actualItems[num].setStartTime(start);
    actualItems[num].setEndTime(end);
}

void RunningTable::printCompleteTable() {
    printf("编号\t起始时间\t结束时间\t实际发车\t实际到站\t方向\t实际乘坐\n");
    int i = 0;
    for(i = 0; i < number; i++)
    {
        cout<<i+1<<"\t"<<dueItems[i].getStartTime()<<"\t"<<dueItems[i].getEndTime()<<"\t"<<actualItems[i].getStartTime()<<"\t"<<actualItems[i].getEndTime();
        if(dueItems[i].getDirection())
        {
            cout<<"\t返\t";
        }else
        {
            cout<<"\t往\t";
        }
        cout<<actualItems[i].getActualCarring()<<"人"<<endl;
    }
}

double RunningTable::getCarryRate(int dueToCarry) {
    int i = 0;
    double result;
    for(i = 0; i < number; i++)
    {
        result += actualItems[i].getActualCarring()/dueToCarry;
    }
    return result;
}

double RunningTable::getTimeRate() {
    int i = 0;
    double result = 1;
    for(i = 0; i < number; i++)
    {
        Time dstart = dueItems[i].getStartTime();
        Time astart = actualItems[i].getStartTime();
        int delta = astart - dstart;
        //认为两分钟左右发车为准点
        if(delta*delta >= 4){
            result -= 1/number;
        }
    }
    return result;
}

